import {ForbiddenException, Injectable} from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm";
import {uploadImage} from "./entity/uploadimage.entity";
import {Repository} from "typeorm";
import  {UploadFileDto} from './dto/uploadFile.dto'

@Injectable()
export class ImageFieldService {
    constructor(
        @InjectRepository(uploadImage)
        private imageRepository:Repository<uploadImage>
    ) {}

    async getImage(imageId:string){
        const imageBase64 =await this.imageRepository.findOne({where:{miniUid:imageId}})
        if (!imageBase64) { throw new ForbiddenException('图片查询错误'); }
        return imageBase64;
    }

    async pushImage(image: UploadFileDto) {
        const info: uploadImage = {
            id: undefined,
            miniUid:image.miniUid,
            time:image.time,
            data:image.data,
            type:image.type
        }
        const newImage = this.imageRepository.create(info);
        return await this.imageRepository.save(newImage);
    }
}
